package grp.test.service.Impl;

import grp.test.common.ServerResponse;
import grp.test.dao.BookMapper;
import grp.test.pojo.Book;
import grp.test.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("iBookService")
public class BookServiceImpl implements IBookService {
    @Autowired(required=false)
    private BookMapper bookMapper;
    @Override
    public ServerResponse<List<Book>> bookList()
    {
        List<Book> booklist = bookMapper.selectAllBy();
        if(booklist == null)
        {
            return ServerResponse.createByErrorMessage("无书籍");
        }

        return ServerResponse.createBySuccess("数据读取成功",booklist);
    }

    public ServerResponse<Book> book_add(Book newBook)
    {
        Book book = bookMapper.selectByPrimaryKey(newBook.getBookId());

        if(book == null){
            book = new Book(newBook.getBookId(),newBook.getBookName(),newBook.getPrice(),newBook.getStore(),newBook.getDes(),newBook.getBookType());
            bookMapper.insert(book);
        }
        else{
//            int count = book.getStore()+newBook.getStore();
//            book.setStore(count);
//            bookMapper.updateByPrimaryKey(book);
            return ServerResponse.createByErrorMessage("书籍已存在，请去更新书籍界面");
        }
        return ServerResponse.createBySuccess("添加成功",book);

    }
    public ServerResponse<String> book_delete(Integer bookId)
    {
        Book book = bookMapper.selectByPrimaryKey(bookId);
        if (book != null) {
            bookMapper.deleteByPrimaryKey(bookId);
        }
        else
           return ServerResponse.createByErrorMessage("书籍不存在！");

        return ServerResponse.createBySuccess("删除成功");
    }

}
